package socketTest;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;



public class SyncClient {
	class Types {
		public static final int IO_START = 0xFF01;
		public static final int IO_END = 0xFF02;
		public static final int MON = 0xFFFF; // for monitor: living?
		public static final int URL = 0x0001;
		public static final int CONTENT = 0x0101;

	}
	protected String host;
	protected int port;
	protected static String encoding = "GBK";
	protected Socket socket;

	public SyncClient(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public Socket connect() throws UnknownHostException, IOException {
		long start = System.currentTimeMillis();
		socket = new Socket();
		//socket.setSoTimeout(1000);
        InetSocketAddress add = new InetSocketAddress(host,port);
        socket.connect(add,1000);
		return this.socket;
	}

	public void request(String url) throws IOException {
		request(socket.getOutputStream(), url);
	}

	public void request(OutputStream out, String url) throws IOException {
		DataOutputStream dout = new DataOutputStream(out);
		write(dout, Types.IO_START, 1);
		write(dout, Types.URL, url);
		write(dout, Types.IO_END, null);
		out.flush();
	}

	public String[] responseUrlCnt() throws IOException {
		return responseUrlCnt(socket.getInputStream());
	}

	public String[] responseUrlCnt(InputStream in) throws IOException {
		String[] ret = new String[2];
		DataInputStream din = new DataInputStream(in);
		int len = din.readInt();
		byte[] data = new byte[len];
		din.readFully(data);
		din = new DataInputStream(new ByteArrayInputStream(data));
		while (din.available() > 5) {
			int T = din.readUnsignedShort();
			int L = din.readInt();
			switch (T) {
			case Types.IO_START:
				din.readByte();
				break;
			case Types.IO_END:
				break;
			case Types.URL:
				ret[0] = read(din, L);
				break;
			case Types.CONTENT:
				ret[1] = read(din, L);
				break;
			default:
				din.skipBytes(L);
				break;
			}
		}
		return ret;
	}

	public void write(DataOutputStream out, int type, byte b)
			throws IOException {
		out.writeShort(type);
		out.writeInt(1);
		out.writeByte(b);
	}

	public void write(DataOutputStream out, int type, int i) throws IOException {
		out.writeShort(type);
		out.writeInt(4);
		out.writeInt(i);
	}

	public void write(DataOutputStream out, int type, String s)
			throws IOException {
		byte[] data = s == null ? null : s.getBytes(encoding);
		out.writeShort(type);
		out.writeInt(data == null ? 0 : data.length);
		if (data != null)
			out.write(data);
	}

	public String read(DataInputStream in, int len) throws IOException {
		byte[] data = new byte[len];
		in.readFully(data);
		return new String(data, encoding);
	}

	public void close() throws IOException {
		socket.close();
	}

	public String echo(String value) {
		if (value != null && value.length() > 0)
			try {
				DataOutputStream out = new DataOutputStream(socket
						.getOutputStream());
				write(out, Types.IO_START, 1);
				write(out, Types.MON, value);
				write(out, Types.IO_END, null);
				out.flush();

				DataInputStream in = new DataInputStream(socket
						.getInputStream());
				do {
					int t = in.readUnsignedShort();
					int l = in.readInt();
					switch (t) {
					case Types.IO_START:
						in.readByte();
						break;
					case Types.IO_END:
						break;
					case Types.MON:
						return read(in, l);

					default:
						in.skip(l);
						break;
					}
				} while (in.available() > 5);
			} catch (IOException e) {
				e.printStackTrace();
			}
		return "";
	}

	public static void main(String[] args) throws UnknownHostException,
			IOException {
		String host = "202.108.1.121";
		int port = 6970;
		String keyword = "七天酒店";
		keyword = URLEncoder.encode(keyword, "gbk");
//		String msg = "http://g.zhongsou.com/3rdtemplate_" + keyword;
		String msg = "http://g.zhongsou.com/3rdsearch_" + keyword;
		SyncClient c = new SyncClient(host, port);
		c.connect();
		msg=new String(msg.getBytes("ISO-8859-1"));
		//请求
		c.request(msg);
		//接收请求
		String[] ret = c.responseUrlCnt();
		c.close();
		if (msg.equals(ret[0]) && ret[1] != null){
			System.out.println(ret[1]);
		}
	}
}
